package com.hyc.DataStructure.Search;

/**
 * @projectName: DataStructure
 * @package: com.hyc.DataStructure.Search
 * @className: indexValueSearch
 * @author: 冷环渊 doomwatcher
 * @description: TODO
 * @date: 2022/1/29 16:40
 * @version: 1.0
 */
public class indexValueSearch {
    public static void main(String[] args) {
        int[] arr = {1, 3, 9, 10, 12, 14, 15, 1000, 1000, 1234};
        System.out.println(indexValueSearch(arr, 0, arr.length - 1, 1234));
    }

    public static int indexValueSearch(int arr[], int left, int right, int findVal) {
        System.out.println("hello");
        //这里的条件必须有 findVal < arr[0] || findVal > arr[arr.length - 1] 不然会出现边缘问题，可能越界
        if (left > right || findVal < arr[0] || findVal > arr[arr.length - 1]) {
            return -1;
        }
        int mid = left + (right - left) * (findVal - arr[left]) / (arr[right] - arr[left]);
        int midval = arr[mid];
        if (findVal > midval) {
            return indexValueSearch(arr, mid + 1, right, findVal);
        } else if (findVal < midval) {
            return indexValueSearch(arr, left, mid - 1, findVal);
        } else {
            return mid;
        }
    }
}
